package com.yzyz.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.yzyz.bean.scenic.DescriptionBean;
import com.yzyz.dao.DescriptionDao;
import com.yzyz.utils.mapper.DescriptionRowMapper;

/**
 * 
 * @author David Sun
 * @Date   2010-4-4
 *
 */
public class DescriptionDaoImpl implements DescriptionDao {

	private JdbcTemplate jdbcTemplate;
	@SuppressWarnings("unchecked")
	@Override
	public List<DescriptionBean> getAllDescription() {
		String sql = "select * from description";
		return this.jdbcTemplate.query(sql, new DescriptionRowMapper());
	}

	@Override
	public int delete(int id) {
		String sql = "delete from description where id=" + id;
		return this.jdbcTemplate.update(sql);

	}

	@SuppressWarnings("unchecked")
	@Override
	public Object get(int id) {
		String sql = "select * from description where id=" + id;
		List<DescriptionBean> descs = this.jdbcTemplate.query(sql, new DescriptionRowMapper());
		return descs == null? null: descs.size()==0?null:descs.get(0);
	}

	@Override
	public int save(Object object) {
		final String sql = "insert into description (name, contents, date, ownerId) values(?,?,?,?)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		final DescriptionBean d = (DescriptionBean)object;
		this.jdbcTemplate.update(new PreparedStatementCreator() {
			public java.sql.PreparedStatement createPreparedStatement(
					Connection conn) throws SQLException {
				java.sql.PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				ps.setString(1, d.getName());
				ps.setString(2, d.getContents());
				ps.setTimestamp(3, new java.sql.Timestamp(new Date().getTime()));
				ps.setInt(4, d.getOwnerId());
				return ps;
			}
		}, keyHolder);
		return keyHolder.getKey().intValue();
	}

	@Override
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;

	}

	@Override
	public void update(Object object) {
		String sql = "update description set contents=? where ownerId=?";
		final DescriptionBean d = (DescriptionBean)object;
		this.jdbcTemplate.update(sql, new PreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1,d.getContents());
				ps.setInt(2, d.getOwnerId());
			}});

	}

	@Override
	public int updateAvailable(int id, boolean available) {
		return 0;
	}

	@Override
	public int getCount(int ownerId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public DescriptionBean getByScenic(int scenicId) {
		String sql = "select * from description where ownerId=" + scenicId;
		List<DescriptionBean> list = this.jdbcTemplate.query(sql, new DescriptionRowMapper());
		return list==null?null:list.size()==0?null:(DescriptionBean)list.get(0);
	}

}
